/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

package scm;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author Łukasz Synówka
 */
public class DodawanieProcedury extends JFrame {

    private Connection c = null;
    DefaultListModel listModel = new DefaultListModel();

    /** Creates new form DodawanieProcedury */
    public DodawanieProcedury() {
        initComponents();
        wypelnij_jComboBox1();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList(listModel);
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N


        jLabel1.setName("jLabel1");
        jLabel1.setText("Nazwa procedury");

        jLabel2.setName("jLabel2");
        jLabel2.setText("Dodaj instrukcję");

        jLabel3.setName("jLabel1");
        jLabel3.setText("Usuń instrukcję");

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        /*jList1.setModel(new javax.swing.AbstractListModel() {
            String[] s={"a","c"};
            public int getSize() { return s.length; }
            public Object getElementAt(int i) { return s[i]; }
        });*/
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        
        jButton1.setText("Ok"); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Anuluj"); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        jComboBox2.setName("jComboBox2"); // NOI18N

        jButton3.setText("Dodaj"); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Usuń"); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        jTextField1.setText(""); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jComboBox2, 0, 152, Short.MAX_VALUE)
                    .addComponent(jComboBox1, 0, 152, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addGap(3, 3, 3)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addGap(5, 5, 5)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        Object o=this.jComboBox1.getSelectedItem();
        this.jComboBox2.addItem(o);
        listModel.addElement(o);

    }

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        int i = this.jComboBox2.getSelectedIndex();
        this.jComboBox2.removeItemAt(i);
        this.listModel.removeElementAt(i);
    }

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
    {
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
             c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

             PreparedStatement ps = c.prepareStatement("INSERT INTO procedures(name) VALUES(?)");
             ps.setString(1, jTextField1.getText());
             int rows_affected = ps.executeUpdate();
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }

        String id = "0";
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
             c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

             PreparedStatement ps = c.prepareStatement("SELECT id FROM procedures WHERE name = ?");
             ps.setString(1, jTextField1.getText());
             ResultSet rs = ps.executeQuery();


             while (rs.next())
             {
                 id = rs.getString("id");
             }

        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }

        for (int i=0; i<jList1.getModel().getSize(); i++)
        {
            String entry = (String) jList1.getModel().getElementAt(i);
            if (!entry.equals(""))
            {
                String[] parts = entry.split("\\.");
                try
                {
                    Class.forName("org.postgresql.Driver");
                }
            catch (ClassNotFoundException cnfe)
            {
                JOptionPane.showMessageDialog(null, cnfe.getMessage());
            }
            try
            {
                c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

                PreparedStatement ps = c.prepareStatement("INSERT INTO proc_act(id_proc, id_act) VALUES(?, ?)");
                ps.setInt(1, Integer.parseInt(id));
                ps.setInt(2, Integer.parseInt(parts[0]));
                int rows_affected = ps.executeUpdate();
            }
            catch (SQLException se)
            {
                JOptionPane.showMessageDialog(null, se.getMessage());
            }
            }

        }     

        this.dispose();
    }

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        this.dispose();
    }
    private void wypelnij_jComboBox1()
    {
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("SELECT id, name FROM actions");
            ResultSet rs = ps.executeQuery();
            while (rs.next())
            {
                 jComboBox1.addItem(rs.getString("id")+"."+rs.getString("name"));
            }
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration
}

